package com.yang.ddd.shared.start;

import com.yang.ddd.MainApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
public class StartApplication {
    public static ConfigurableApplicationContext run(Class<?> clazz, String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
        Boolean swaggerEnable = context.getEnvironment().getProperty("knife4j.enable", Boolean.class, true);
        Integer port = context.getEnvironment().getProperty("server.port", Integer.class, 8080);
        String hostAddress = null;
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ignore) {
        }
        log.info("本地地址: http://localhost:{}", port);
        log.info("外部地址: http://{}:{}", hostAddress, port);
        if (Boolean.TRUE.equals(swaggerEnable)) {
            log.info("接口文档（knife4j）: http://{}:{}/api/doc.html", hostAddress, port);
        }
        return context;
    }
}
